/**
 * \* Created with IntelliJ IDEA.
 * \* User: Administrator
 * \* Date: 2021/11/27
 * \* Time: 20:39
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class TestDemo {

    static class MyException extends Exception{
        public MyException(String msg){
            super(msg);//调用Exception类的构造方法，存入异常信息
        }
    }
    public static void main7(String[] args) {
        try{
            throw new MyException("自定义异常");

        } catch (MyException e) {
            e.printStackTrace();
            System.out.println(e);
        }
    }
    public static void main6(String[] args) {
        try {
            //抛出异常的实例化对象
            throw  new ArrayIndexOutOfBoundsException("\n我是手工异常：\n数组下标越界");
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println(e);
        }
    }


    //定义私有化静态方法，并使用throws抛出异常
    //一旦方法出现异常，setZero（）方法自己并不处理，而是将异常提交给它的上级调用这main()方法
    private static void setZero(int[] arr,int index)throws ArrayIndexOutOfBoundsException{
        arr[index]=0;
    }
    public static void main5(String[] args) {
        int []arr=new int[5];
        try{
            setZero(arr,10);
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("数组超出绑定方位！");
            System.out.println("异常："+e);
        }
        System.out.println("main()方法结束！");
    }
    public static void main4(String[] args) {//
        int[] arr = {1, 2, 3};
        try{
            System.out.println(arr[10]);
        }catch (ArrayIndexOutOfBoundsException e){
            System.out.println("我在这里捕捉到一个异常");
            e.printStackTrace();
        }
        System.out.println("hello!!!");

    }
    public static void main3(String[] args) {
        String str=null;
        System.out.println(str.length());
    }
    public static void main2(String[] args) {
        int []arr=new int[4];
        System.out.println(arr[10]);
    }
    public static void main1(String[] args) {
        //1.算术异常
//        System.out.println(10/0);
    }
}
